package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.JMException;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.monitor.SpiderMonitor;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.ManagerGuquanProcessor;
import tech.abing.spider.task.pipeline.amac.ManagerGuquanPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 抓取自主发行类型私募基金管理人
 * 
 * @author Ivan qb.huang@wescxx.com
 * @date 2016-10-31 下午02:53:59
 * @version V2.0
 */
public class ManagerGuquanSpider {

	/**
	 * @param args
	 */
	private static SqlUtil sqlUtil = null;

	public static void start(int thread, int politenessDelay) {

		CrawlManager manager = CrawlManager.getInstance();

		CrawTaskBuilder builder = manager
				.prepareCrawlTask(DictEnum.TASK_TYPE_FUND_MANAGER_GUQUAN.getName(), DefaultPageDownloader.class)
				.useThread(thread).useDynamicEntrance(DynamicEntranceImpl.class).usePageEncoding(PageEncoding.UTF8)
				.usePageRetryCount(3)
				// .setPolitenessDelay(1000)
				.usePipeline(ManagerGuquanPipeline.class).setPolitenessDelay(politenessDelay)
				// .useProxyIpPool(MyProxyIpPool.class, 100, 1000 * 60 * 30, -1)//每次网络请求10个代理ip,ip30分钟后过期,有效的ip不限制使用次数
				.useTaskLifeListener(new TaskLifeListener() {

					@Override
					public void onStart(CrawlTask task) {
						if (sqlUtil == null) {
							sqlUtil = SqlUtil.getInstance();
						}
						sqlUtil.deleteForConn("delete from " + DictEnum.TASK_TYPE_FUND_MANAGER_GUQUAN.getCode());
						System.out.println("================开始");
					}

					@Override
					public void onFinished(CrawlTask task) {
						task.getPipeline().destory();
						sqlUtil.closeConn();
						sqlUtil = null;
						System.out.println("================完成");
					}

				});

		CrawlTask task = builder.build();

		try {
			SpiderMonitor.instance().register(task);
		} catch (JMException e) {
			e.printStackTrace();
		}

		manager.start(task);
	}

	public static void start() {
		start(20, 200);
	}

	public static final class DynamicEntranceImpl extends DynamicEntrance {

		@Override
		public List<StartContext> loadStartContext() {

			StartContext context = new StartContext();

			for (int i = 1; i < 6; i++) {
				PageRequest req = buildReq(context);
				req.setReqPayloadParams(getReqPayloadParams(i));
				req.setRequestParams(getRequestParams(i));
				context.injectSeed(req);
			}

			return Arrays.asList(context);
		}

		private PageRequest buildReq(StartContext context) {
			String rand = String.valueOf(Math.random());
			String url = "http://gs.amac.org.cn/amac-infodisc/api/pof/manager?rand=" + rand + "&page=0&size=20";

			PageRequest req = context.createPageRequest(url, ManagerGuquanProcessor.class);
			req.setMethod(Method.POST);
			req.setPostType(PostType.REQUEST_PAYLOAD);
			req.setReqPayloadParams("{}");

			req.putHeader("Content-Type", "application/json");
			req.putHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
			req.putHeader("Referer", "http://gs.amac.org.cn/amac-infodisc/res/pof/manager/managerList.html");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			req.putHeader("Host", "gs.amac.org.cn");
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");

			return req;
		}

		private Map getRequestParams(int level) {

			Map params = new HashMap<String, String>();
			params.put("level", level + "");
			return params;
		}

		private String getReqPayloadParams(int level) {
			String reqPayloadParams = "";
			switch (level) {
			case 1:
				reqPayloadParams = "{\"fundScale\":{\"to\":\"200000\", \"from\":\"0\"}, \"fundType\":\"私募股权\"}";
				break;
			case 2:
				reqPayloadParams = "{\"fundScale\":{\"to\":\"500000\", \"from\":\"200000\"}, \"fundType\":\"私募股权\"}";
				break;
			case 3:
				reqPayloadParams = "{\"fundScale\":{\"to\":\"1000000\", \"from\":\"500000\"}, \"fundType\":\"私募股权\"}";
				break;
			case 4:
				reqPayloadParams = "{\"fundScale\":{\"from\":\"1000000\"}, \"fundType\":\"私募股权\"}";
				break;

			default:
				break;
			}
			return reqPayloadParams;
		}

	}

}
